/**
 * 
 */
package edi.android.tmdbBrowser;

import java.text.SimpleDateFormat;
import java.util.Locale;

import net.sf.jtmdb.Movie;
import android.app.Application;
import edi.android.library.ImageDownloader;

/**
 * @author edi
 * 
 */
public class TMDbApp extends Application {
	// Image Downloader and cacher
	public final static ImageDownloader imageDownloader = new ImageDownloader();
	public final static MovieDownloader movieDownloader = new MovieDownloader();
	public final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Application#onCreate()
	 */
	@Override
	public void onCreate() {
		super.onCreate();
		// set the TMDB api key
		net.sf.jtmdb.GeneralSettings
				.setApiKey("e9f8c7cbe0159950e3c1c1a8fe8573e5");
		// set the current locale to get locale movies
		net.sf.jtmdb.GeneralSettings.setAPILanguage(Locale.getDefault()
				.getLanguage());
	}

	/**
	 * @return the imageDownloader
	 */
	public ImageDownloader getImageDownloader() {
		return imageDownloader;
	}

	/**
	 * @param movie The movie
	 * @param setPrefix Should prefix be used?
	 * @return Secondary information or null, inf none
	 */
	public String chooseSecondaryName(Movie movie, boolean setPrefix) {
		final String name = movie.getName();
		final String original = movie.getOriginalName();
		final String alternative = movie.getAlternativeName();
		String secondary;

		if (original.equalsIgnoreCase("null")
				&& alternative.equalsIgnoreCase("null")) {
			return "null";
		}

		if (!original.equalsIgnoreCase(name)
				&& !original.equalsIgnoreCase("null")) {
			secondary = original;
			if (setPrefix) {
				secondary = getString(R.string.prefixOriginal) + " "
						+ secondary;
			}
			return secondary;
		}

		if (!alternative.equalsIgnoreCase(name)
				&& !alternative.equalsIgnoreCase("null")) {
			secondary = alternative;
			if (setPrefix) {
				secondary = getString(R.string.prefixAlternative) + " "
						+ secondary;
			}
			return secondary;
		}
		return "null";
	}

}
